#include<iostream>
#include<cmath>
using namespace std;
long long b[131072],c[100000],q[100000][2],q2[17][100000],r=0,r2=0;
string vot[17];
int main(){
    freopen("arena.in","r",stdin);
   freopen("arena.out","w",stdout);
    int a,a2,a3,a4;
    cin>>a>>a2;
    a4=ceil(log2(a));
    for(int d=0;d<a;d++){
        cin>>b[d];
    }
    for(int d=a;d<(1<<a4);d++){
        b[d]=-1;
    }
    for(int e=0;e<a2;e++){
        cin>>c[e];
        c[e]--;
    }
    for(int e=0;e<a4;e++){
        cin>>vot[e];
    }
    cin>>a3;
    for(int f=0;f<a3;f++){
        int g[4];
        cin>>g[0]>>g[1]>>g[2]>>g[3];
        for(int h=0;h<a;h++){
            b[h]^=g[(h+1)%4];
        }
        for(int h=0;h<a;h++){
            q[h][0]=q[h][1]=0;
            r=h,r2=ceil(log2(h+1));
            for(int i=0;i<r2;i++){
                r=0;
                for(int j=0;j<(1<<(r2-i-1));j++){
                    if(i==0){
                        if(vot[i][j]=='0'){
                            if(2*j>h){
                                q[h][1]+=2*j+2*j+1+2;
                                q2[i][j]=-1;
                            }else if(b[2*j]>=i+1){
                                q2[i][j]=2*j;
                            }else{
                                q2[i][j]=2*j+1;
                            }
                        }else{
                            if(2*j+1>h){
                                q[h][1]+=2*j+2*j+1+2;
                                q2[i][j]=-1;
                            }else if(b[2*j+1]>=i+1){
                                q2[i][j]=2*j+1;
                            }else{
                                q2[i][j]=2*j;
                            }
                        }
                    }else{
                        if(vot[i][j]=='0'){
                            if(q2[i-1][2*j]>h){
                                q[h][1]+=q2[i-1][2*j]+q2[i-1][2*j+1]+2;
                                q2[i][j]=-1;
                            }else if(b[q2[i-1][2*j]]>=i+1){
                                q2[i][j]=q2[i-1][2*j];
                            }else{
                                q2[i][j]=q2[i-1][2*j+1];
                            }
                        }else{
                            if(q2[i-1][2*j+1]>h){
                                q[h][1]+=q2[i-1][2*j]+q2[i-1][2*j+1]+2;
                                q2[i][j]=-1;
                            }else if(b[q2[i-1][2*j+1]]>=i+1){
                                q2[i][j]=q2[i-1][2*j+1];
                            }else{
                                q2[i][j]=q2[i-1][2*j];
                            }
                        }
                    }
                }
            }
            if(r2!=0){
            q[h][0]=q2[r2-1][0]+1;
            }else{
                q[h][0]=1;
            }
        }
        long long csr=0,ba=1;
        for(int h=0;h<a2;h++){
            csr^=ba*(h+1)*(q[c[h]][0]+q[c[h]][1]);
        }
        for(int h=0;h<a;h++){
            b[h]^=g[(h+1)%4];
        }
        for(int i=0;i<a4;i++){
            for(int j=0;j<a;j++){
                q2[i][j]=0;
            }
        }
        cout<<csr<<endl;
    }
    return 0;
}
